using System;
using System.Text;

namespace FastMapper.Helpers
{
    public class MemberInfoNode
    {
        public void AddParent(MemberInfoNode parent)
        {
            if (parent == null)
                throw new ArgumentNullException("parent");
            parent.Child = this;
            Parent = parent;
        }

        public string GetFullPath()
        {
            var strBuilder = new StringBuilder(Name);
            var child = Child;
            while(child != null)
            {
                strBuilder.AppendFormat(".{0}", child.Name);
                child = child.Child;
            }
            return strBuilder.ToString();
        }
        public MemberInfoNode GetRootMemberInfoNode()
        {
            if (IsRoot)
                return this;
            return Parent.GetRootMemberInfoNode();
        }
        
        public void AddChild(MemberInfoNode child)
        {
            if (child == null)
                throw new ArgumentNullException("child");
            child.Parent = this;
            Child = child;
        }

        public bool IsRoot
        {
            get { return Parent == null; }
        }
        public MemberInfoNode Parent { get; private set; }
        public string Name { get; set; }
        public Type MemberType { get; set; }
        public MemberInfoNode Child { get; private set; }

    }
}